import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/store/auth'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

// 配置进度条
NProgress.configure({ showSpinner: false })

const routes = [
  {
    path: '/test',
    name: 'Test',
    component: () => import('@/views/Test.vue'),
    meta: {
      title: '测试页面',
      requiresAuth: false
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: {
      title: '登录',
      requiresAuth: false
    }
  },
  {
    path: '/',
    name: 'Layout',
    component: () => import('@/components/Layout/MainLayout.vue'),
    redirect: '/dashboard',
    meta: {
      requiresAuth: true
    },
    children: [
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: () => import('@/views/Dashboard.vue'),
        meta: {
          title: '控制面板',
          icon: 'Dashboard',
          requiresAuth: true
        }
      },
      {
        path: '/configs',
        name: 'ConfigList',
        component: () => import('@/views/ConfigList.vue'),
        meta: {
          title: '全局配置',
          icon: 'Setting',
          requiresAuth: true
        }
      },
      {
        path: '/configs/edit/:id?',
        name: 'ConfigEdit',
        component: () => import('@/views/ConfigEdit.vue'),
        meta: {
          title: '配置编辑',
          hidden: true,
          requiresAuth: true
        }
      },
      {
        path: '/accounts',
        name: 'AccountList',
        component: () => import('@/views/AccountList.vue'),
        meta: {
          title: '账号管理',
          icon: 'User',
          requiresAuth: true
        }
      },
      {
        path: '/accounts/edit/:id?',
        name: 'AccountEdit',
        component: () => import('@/views/AccountEdit.vue'),
        meta: {
          title: '账号编辑',
          hidden: true,
          requiresAuth: true
        }
      },
      {
        path: '/accounts/:accountId/configs',
        name: 'AccountConfigList',
        component: () => import('@/views/AccountConfigList.vue'),
        meta: {
          title: '账号配置',
          hidden: true,
          requiresAuth: true
        }
      },
      {
        path: '/accounts/:accountId/configs/edit/:configId?',
        name: 'AccountConfigEdit',
        component: () => import('@/views/AccountConfigEdit.vue'),
        meta: {
          title: '账号配置编辑',
          hidden: true,
          requiresAuth: true
        }
      },
      {
        path: '/migration',
        name: 'MigrationManager',
        component: () => import('@/views/MigrationManager.vue'),
        meta: {
          title: '数据迁移',
          icon: 'Upload',
          requiresAuth: true
        }
      },
      {
        path: '/system/settings',
        name: 'SystemSettings',
        component: () => import('@/views/SystemSettings.vue'),
        meta: {
          title: '系统设置',
          icon: 'Tools',
          requiresAuth: true
        }
      },
      {
        path: '/system/scripts',
        name: 'ScriptManagement',
        component: () => import('@/views/ScriptManagement.vue'),
        meta: {
          title: '脚本管理',
          icon: 'Document',
          requiresAuth: true
        }
      },
      {
        path: '/monitor/abnormal-transactions',
        name: 'AbnormalTransactions',
        component: () => import('@/views/AbnormalTransactions.vue'),
        meta: {
          title: '异常交易',
          icon: 'Warning',
          requiresAuth: true
        }
      },
      {
        path: '/change-password',
        name: 'ChangePassword',
        component: () => import('@/views/ChangePassword.vue'),
        meta: {
          title: '修改密码',
          icon: 'Key',
          hidden: true,
          requiresAuth: true
        }
      }
    ]
  },
  {
    path: '/404',
    name: 'NotFound',
    component: () => import('@/views/404.vue'),
    meta: {
      title: '页面未找到'
    }
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 简化的路由守卫
router.beforeEach(async (to, from, next) => {
  NProgress.start()

  // 设置页面标题
  if (to.meta.title) {
    document.title = `${to.meta.title} - Web3项目管理工具`
  }

  const authStore = useAuthStore()

  if (to.path === '/login' && authStore.isAuthenticated) {
    next({ path: '/dashboard' })
    return
  }

  if (to.meta.requiresAuth !== false) {
    const isAuthed = await authStore.checkLoginStatus()
    if (!isAuthed) {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
      return
    }
  }

  next()
})

router.afterEach(() => {
  NProgress.done()
})

export default router
